package com.jiangjiang.eshop.order.price.v1.calculator.impl;

import com.jiangjiang.eshop.commodity.dto.GoodsSkuDTO;
import com.jiangjiang.eshop.commodity.service.CommodityService;
import com.jiangjiang.eshop.logistics.service.LogisticsFacadeService;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.order.dto.PromotionActivityResult;
import com.jiangjiang.eshop.order.price.v1.calculator.FreightCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jianghaowen
 * @Date 2021/7/14
 * @Description: 含赠品的运费计算组件
 * @Version V1.0
 **/
@Component
public class GiftIncludedFreightCalculator implements FreightCalculator {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private LogisticsFacadeService logisticsFacadeService;

    /**
     * 含赠品的运费计算
     * @param order 订单
     * @param orderItem 订单条目
     * @param result 促销活动计算结果
     * @return
     */
    @Override
    public Double calculate(OrderInfoDTO order, OrderItemDTO orderItem, PromotionActivityResult result) {
        // 计算商品本身的运费
        Double freight = logisticsFacadeService.calculateFreight(order, orderItem);

        // 汇总赠品运费
        for (OrderItemDTO giftItem : result.getOrderItems()) {
            freight += logisticsFacadeService.calculateFreight(order,giftItem);
        }
        return freight;
    }
}
